// pages/dutyweekItem/dutyweekItem.js


const app = getApp()
const util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // tab切换
    //  currentTab: 0,
    //  identityId: null,
    //  tabTitle: "待巡查",
    points: 15,
    contents: 12,
    noMore: true, //判断巡查任务是否大于3条标志
    categorys: [],
    loading: 1,
    uuids: [],
    currentMacaddress: "",
    currentcheckAddress: "",
    currentcheckPointId: "",
    dutyWeekBea: [],
    selectedIndex: -1,
    selectedIndex2: 0,
    locationContent: "请在系统设置-应用权限管理中开启允许微信使用地理位置的开关",
    deviceSeleted:false,
    deviceType:["NFC","蓝牙"],
    deviceName:"",
    e_macaddress:"",
    zhizhouErr:0,
    category:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getlist();
    wx.request({
      url: app.zhizhouUrl + '/WeChat/weChatSystem/getSchoolUUIDS',
      method: 'POST',
      data: app.globalData.departmentId,

      dataType: 'json',
      success: res => {
        this.setData({
          uuids: res.data.split(",")
        })
      }
    })
  },
  hideModal2(e) {
    this.setData({
      deviceSeleted:false,
      selectedIndex2: 0,
    })
  },
  todutyList2: util.throttle(function(e){
    this.setData({
      deviceSeleted:true
    })
  }),
  selectType(e){
    let values = e.detail.value.split(',')
    console.log("+++", values)
    this.setData({
      selectedIndex2:values[0]
    })
    console.log("selectIndex2", this.data.selectedIndex2)
  },
  confirmSelection2(e){
    console.log("test", this.data.deviceType[this.data.selectedIndex2]);
    this.setData({
      deviceName:this.data.deviceType[this.data.selectedIndex2],
      deviceSeleted: false
    })
    if(this.data.deviceSeleted==false && this.data.deviceName=="NFC"){
      wx.navigateTo({
        url:`/subpages/pages/nfcUserResponse/nfcUserResponse?categoryName=${this.data.category}`
      })
    }
    else{ //获得该任务待选检查点列表
      let that = this
      wx.request({
        url: app.zhizhouUrl + '/WeChat/weChatSystem/getCheckpointAndCategory',
        method: 'POST',
        data: {
          schoolId: parseInt(app.globalData.departmentId),
          categoryId: this.data.categoryid
        },
        dataType: 'json',
        success: res => {
          console.log("res.data", res.data)
          
          
          if(this.data.deviceSeleted==false && this.data.deviceName=="蓝牙"){
            let that = this;
          let list = res.data.filter((item) => item.isUse == 1)
          console.log("list", list)
          // 需要打开蓝牙
          if (this.data.e_macaddress.indexOf("00000") > -1) {
            wx.navigateTo({
              url: `/pages/dutyweekItemList/dutyweekItemList?zhizhouErr=${this.data.ZhizhouErr}&currentMacaddress=${that.data.currentMacaddress}&currentcheckAddress=${that.data.currentcheckAddress}&currentcategoryid=${e.currentTarget.dataset.categoryid}&currentcheckPointId=${that.data.currentcheckPointId}&currentcategory=${e.currentTarget.dataset.category}`
            })
            that.setData({
              fromIndex: 0
            })
          } else {
            this.setData({
              stop: 0
            })
            if (wx.openBluetoothAdapter) {
              wx.showLoading({
                title: '正在搜索蓝牙',
                mask: true,
                showCancel: true
              })
              wx.openBluetoothAdapter({
                //开启蓝牙
                fail(res) {
                  wx.showToast({
                    title: "请开启蓝牙",
                    image: "/images/warning.png",
                    duration: 2000
                  })
                },
                success(res) {
                  //允许打开定位
                  console.log("==============进入蓝牙")
                  wx.startBeaconDiscovery({
                    uuids: that.data.uuids,
                    // uuids: ["00001803-494c-4f47-4943-544543480000"],
                    success(res) {
                      console.log("res1", res)
  
                      // wx.onBeaconUpdate(function (res) {
                      //   console.log("开始监听~",res.beacons);
                      //   if (res.beacons.length) {
                      //     console.log("res.beacons", res.beacons);
                      //     console.log(" list", list);
                      //     var find = 0
                      //     for (let i = 0; i < res.beacons.length; i++) {
                      //       console.log("minor",res.beacons[i].minor)
                      //       console.log("accuracy",res.beacons[i].accuracy)
                      //       for (let j = 0; j < list.length; j++) {
                      //         if (`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}` == list[j].macAddress) {
                      //           console.log("macAddress", list[j].macAddress);
                      //           that.setData({
                      //             currentMacaddress: list[j].macAddress,
                      //             currentcheckAddress: list[j].address,
                      //             currentcheckPointId: list[j].id
                      //           })
                      //           find = 1
                      //           break
                      //         }
                      //       }
                      //       //搜索到的第一个蓝牙距离最近
                      //       if(find == 1){
                      //         break
                      //       }
                      //     }
                      //     if (find == 1) {
                      //       that.setData({
                      //         stop: 1,
                      //         bleConnect: 1
                      //       })
                      //       wx.hideLoading()
                      //       wx.stopBeaconDiscovery()
                      //       wx.showModal({
                      //         title: '提示',
                      //         content: '是否在' + that.data.currentcheckAddress + '开始值周',
                      //         success(res) {
                      //           if (res.confirm) {
                      //             wx.navigateTo({
                      //               url: `/pages/dutyweekItemList/dutyweekItemList?currentMacaddress=${that.data.currentMacaddress}&currentcheckAddress=${that.data.currentcheckAddress}&currentcategoryid=${e.currentTarget.dataset.categoryid}&currentcheckPointId=${that.data.currentcheckPointId}&currentcategory=${e.currentTarget.dataset.category}`
                      //             })
                      //           } else if (res.cancel) {
                      //             console.log('用户点击取消')
                      //           }
                      //         }
                      //       })
                      //     } else {
                      //       wx.hideLoading()
                      //       wx.stopBeaconDiscovery()
                      //       that.showModalOwn()
                      //     }
                      //   } else if (!res.beacons) {
                      //     console.log("首次搜索shibai", res.beacons);
                      //     wx.offBeaconUpdate((res) => {
                      //       console.log(取消监听);
                      //     })
                      //     wx.hideLoading()
                      //     wx.showModal({
                      //       title: '未搜索到蓝牙',
                      //       content: '未搜索到值周检查点蓝牙，是否重新搜索',
                      //       success(res) {
                      //         if (res.confirm) {
  
                      //           wx.showLoading({
                      //             title: '正在搜索蓝牙',
                      //             mask: true,
                      //             showCancel: false
                      //           })
                      //           wx.onBeaconUpdate(function (res) {
                      //             console.log("zai次搜索", res.beacons);
                      //             if (res.beacons.length) {
                      //               var find = 0
                      //               for (let i = 0; i < res.beacons.length; i++) {
                      //                 for (let j = 0; j < list.length; j++) {
                      //                   // `${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}
                      //                   if (`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}` == list[j].macAddress) {
                      //                     that.setData({
                      //                       currentMacaddress: list[j].macAddress,
                      //                       currentcheckAddress: list[j].address,
                      //                       currentcheckPointId: list[j].id
                      //                     })
                      //                     find = 1
                      //                     break
                      //                   }
                      //                 }
                      //                 break
                      //               }
                      //               if (find == 1) {
                      //                 that.setData({
                      //                   stop: 1,
                      //                   bleConnect: 1
                      //                 })
  
                      //                 wx.stopBeaconDiscovery()
                      //                 wx.hideLoading()
                      //                 wx.showModal({
                      //                   title: '提示',
                      //                   content: '是否在' + that.data.currentcheckAddress + '开始值周',
                      //                   success(res) {
                      //                     if (res.confirm) {
                      //                       wx.navigateTo({
                      //                         url: `/pages/dutyweekItemList/dutyweekItemList?currentMacaddress=${that.data.currentMacaddress}&currentcheckAddress=${that.data.currentcheckAddress}&currentcategoryid=${e.currentTarget.dataset.categoryid}&currentcheckPointId=${that.data.currentcheckPointId}&currentcategory=${e.currentTarget.dataset.category}`
                      //                       })
                      //                     } else if (res.cancel) {
                      //                       console.log('用户点击取消')
                      //                     }
                      //                   }
                      //                 })
                      //               } else {
                      //                 wx.hideLoading()
                      //                 wx.stopBeaconDiscovery()
                      //                 that.showModalOwn()
                      //               }
                      //             }
                      //           })
                      //         } else if (res.cancel) {
                      //           wx.hideLoading()
                      //           wx.stopBeaconDiscovery()
                      //           that.showModalOwn()
                      //         }
                      //       }
                      //     })
                      //   }
                      // })
                      wx.onBeaconUpdate(function (res) {
                        console.log("开始监听~", res.beacons);
                        if (res.beacons.length != 0) {
                          console.log("res.beacons", res.beacons);
                          console.log(" list", list);
                          let dutyWeekBea = []
                          for (let i = 0; i < res.beacons.length; i++) {
                            console.log("minor", res.beacons[i].minor)
                            console.log("accuracy", res.beacons[i].accuracy)
                            for (let j = 0; j < list.length; j++) {
                              if (`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}` == list[j].macAddress) {
                                console.log("macAddress", list[j].macAddress);
                                dutyWeekBea.push({
                                  macAddress: list[j].macAddress,
                                  checkAddress: list[j].address,
                                  checkPointId: list[j].id
                                })
                              }
                            }
                            if(dutyWeekBea.length == 3) {
                              break
                            }
                          }
                          that.setData({
                            dutyWeekBea: dutyWeekBea
                          })
                          if (that.data.dutyWeekBea.length != 0) {
                            that.setData({
                              stop: 1,
                              bleConnect: 1
                            })
                            wx.hideLoading()
                            wx.stopBeaconDiscovery()
                          } else {
                            wx.hideLoading()
                            wx.stopBeaconDiscovery()
                            that.showModalOwn()
                          }
                        } else {
                          console.log("首次搜索shibai", res.beacons);
                          wx.offBeaconUpdate((res) => {
                            console.log(取消监听);
                          })
                          wx.hideLoading()
                          wx.showModal({
                            title: '未搜索到蓝牙',
                            content: '未搜索到值周检查点蓝牙，是否重新搜索',
                            success(res) {
                              if (res.confirm) {
                                wx.showLoading({
                                  title: '正在搜索蓝牙',
                                  mask: true,
                                  showCancel: false
                                })
                                wx.onBeaconUpdate(function (res) {
                                  console.log("zai次搜索", res.beacons);
                                  if (res.beacons.length != 0) {
                                    let dutyWeekBea = []
                                    for (let i = 0; i < res.beacons.length; i++) {
                                      for (let j = 0; j < list.length; j++) {
                                        // `${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}
                                        if (`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}` == list[j].macAddress) {
                                          dutyWeekBea.push({
                                            macAddress: list[j].macAddress,
                                            checkAddress: list[j].address,
                                            checkPointId: list[j].id
                                          })
                                        }
                                      }
                                      if(dutyWeekBea.length == 3) {
                                        break
                                      }
                                    }
                                    that.setData({
                                      dutyWeekBea: dutyWeekBea
                                    })
                                    if (that.data.dutyWeekBea.length != 0) {
                                      that.setData({
                                        stop: 1,
                                        bleConnect: 1
                                      })
                                      wx.stopBeaconDiscovery()
                                      wx.hideLoading()
                                    } else {
                                      wx.hideLoading()
                                      wx.stopBeaconDiscovery()
                                      that.showModalOwn()
                                    }
                                  }
                                })
                              } else if (res.cancel) {
                                wx.hideLoading()
                                wx.stopBeaconDiscovery()
                                that.showModalOwn()
                              }
                            }
                          })
                        }
                      })
                      setTimeout(function () {
                        wx.hideLoading()
                      }, 30000)
                    },
                    fail(res) {
                      console.log("------------------fail") 
                    }
                  })
                  // wx.getLocation({
                  //   type: 'wgs84',
                  //   fail: (res) => {
                  //     //不允许打开定位
                  //     wx.hideLoading()
                  //     wx.getSetting({
                  //       success(res) {
                  //         //开启小程序内定位
                  //         if (res.authSetting["scope.userLocation"] == false) {
                  //           wx.showModal({
                  //             content: '请允许小程序使用地理位置功能！您可以在【我的】页面点击【设置】按钮快捷设置。',
                  //             confirmText: "去设置",
                  //             success: res => {
                  //               if (res.confirm) {
                  //                 wx.switchTab({
                  //                   url: '/pages/mine/mine'
                  //                 })
                  //               }
                  //             }
                  //           })
                  //         } else {
                  //           //开启系统设置定位
                  //           wx.getSystemInfo({
                  //             success: res => {
                  //               if (res.platform == "ios") {
                  //                 that.setData({
                  //                   locationContent: "请在系统设置-微信里开启允许微信使用地理位置的开关"
                  //                 })
                  //               }
                  //               wx.showModal({
                  //                 title: '未允许使用地理位置',
                  //                 content: that.data.locationContent,
                  //                 showCancel: false
                  //               })
                  //             },
                  //             fail: res => {
                  //               wx.showModal({
                  //                 title: '未允许使用地理位置',
                  //                 content: that.data.locationContent,
                  //                 showCancel: false
                  //               })
                  //             }
                  //           })
                  //         }
                  //       }
                  //     })
                  //   },
                  //   success: res => {
                  //     //允许打开定位
                  //     wx.startBeaconDiscovery({
                  //       uuids: that.data.uuids,
                  //       // uuids: ["00001803-494c-4f47-4943-544543480000"],
                  //       success(res) {
                  //         console.log("res1",res)
                  //         wx.showLoading({
                  //           title: '正在搜索蓝牙',
                  //           mask: true,
                  //           showCancel: true
                  //         })
                  //         let search = 0
                  //         wx.onBeaconUpdate(function (res) {
                  //           console.log("开始监听~",res.beacons);
                  //           if (res.beacons.length) {
                  //             console.log("首次搜索", res.beacons);
                  //             for (let i = 0; i < res.beacons.length; i++) {
                  //               var find = 0
                  //               for (let j = 0; j < list.length; j++) {
                  //                 // `${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}
                  //                 if (`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}` == list[j].macAddress) {
                  //                   console.log("list[j]", list[j].macAddress);
                  //                   // let a=`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}`
                  //                   that.setData({
                  //                     currentMacaddress: list[j].macAddress,
                  //                     currentcheckAddress: list[j].address,
                  //                     currentcheckPointId: list[j].id
                  //                   })
                  //                   find = 1
                  //                   break
                  //                 }
                  //               }
                  //             }
                  //             if (find == 1) {
                  //               that.setData({
                  //                 stop: 1,
                  //                 bleConnect: 1
                  //               })
                  //               wx.hideLoading()
                  //               wx.stopBeaconDiscovery()
                  //               wx.showModal({
                  //                 title: '提示',
                  //                 content: '是否在' + that.data.currentcheckAddress + '开始值周',
                  //                 success(res) {
                  //                   if (res.confirm) {
                  //                     wx.navigateTo({
                  //                       url: `/pages/dutyweekItemList/dutyweekItemList?currentMacaddress=${that.data.currentMacaddress}&currentcheckAddress=${that.data.currentcheckAddress}&currentcategoryid=${e.currentTarget.dataset.categoryid}&currentcheckPointId=${that.data.currentcheckPointId}&currentcategory=${e.currentTarget.dataset.category}`
                  //                     })
                  //                   } else if (res.cancel) {
                  //                     console.log('用户点击取消')
                  //                   }
                  //                 }
                  //               })
                  //             } else {
                  //               wx.hideLoading()
                  //               wx.stopBeaconDiscovery()
                  //               that.showModalOwn()
                  //             }
                  //           } else if (!res.beacons) {
                  //             console.log("首次搜索shibai", res.beacons);
                  //             wx.offBeaconUpdate((res) => {
                  //               console.log(取消监听);
                  //             })
                  //             wx.hideLoading()
                  //             wx.showModal({
                  //               title: '未搜索到蓝牙',
                  //               content: '未搜索到值周检查点蓝牙，是否重新搜索',
                  //               success(res) {
                  //                 if (res.confirm) {
  
                  //                   wx.showLoading({
                  //                     title: '正在搜索蓝牙',
                  //                     mask: true,
                  //                     showCancel: false
                  //                   })
                  //                   wx.onBeaconUpdate(function (res) {
                  //                     console.log("zai次搜索", res.beacons);
                  //                     if (res.beacons.length) {
                  //                       for (let i = 0; i < res.beacons.length; i++) {
                  //                         var find = 0
                  //                         for (let j = 0; j < list.length; j++) {
                  //                           // `${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}
                  //                           if ('35:11160:43' == list[j].macAddress) {
                  //                             console.log("list[j]", list[j].macAddress);
                  //                             // let a=`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}`
                  //                             that.setData({
                  //                               currentMacaddress: list[j].macAddress,
                  //                               currentcheckAddress: list[j].address,
                  //                               currentcheckPointId: list[j].id
                  //                             })
                  //                             find = 1
                  //                             break
                  //                           }
                  //                         }
                  //                       }
                  //                       if (find == 1) {
                  //                         that.setData({
                  //                           stop: 1,
                  //                           bleConnect: 1
                  //                         })
  
                  //                         wx.stopBeaconDiscovery()
                  //                         wx.hideLoading()
                  //                         wx.showModal({
                  //                           title: '提示',
                  //                           content: '是否在' + that.data.currentcheckAddress + '开始值周',
                  //                           success(res) {
                  //                             if (res.confirm) {
                  //                               wx.navigateTo({
                  //                                 url: `/pages/dutyweekItemList/dutyweekItemList?currentMacaddress=${that.data.currentMacaddress}&currentcheckAddress=${that.data.currentcheckAddress}&currentcategoryid=${e.currentTarget.dataset.categoryid}&currentcheckPointId=${that.data.currentcheckPointId}&currentcategory=${e.currentTarget.dataset.category}`
                  //                               })
                  //                             } else if (res.cancel) {
                  //                               console.log('用户点击取消')
                  //                             }
                  //                           }
                  //                         })
                  //                       } else {
                  //                         wx.hideLoading()
                  //                         wx.stopBeaconDiscovery()
                  //                         that.showModalOwn()
                  //                       }
                  //                     }
                  //                   })
                  //                 } else if (res.cancel) {
                  //                   wx.hideLoading()
                  //                   wx.stopBeaconDiscovery()
                  //                   that.showModalOwn()
                  //                 }
                  //               }
                  //             })
                  //           }
                  //         })
                  //       }
                  //     })
                  //   },
                  //   fail: res => {
                  //     wx.hideLoading()
                  //   }
                  // })
                }
              })
            } else {
              wx.showModal({
                title: '提示',
                showCancel: false,
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
              })
            }
          }
          // pc测试
          // this.setData({
          //   currentMacaddress:"35:10169:13",
          //   currentcheckAddress:"食堂大厅",
          //   currentcheckPointId:1397,
          // })
          // wx.navigateTo({
          //   url: `/pages/dutyweekItemList/dutyweekItemList?currentMacaddress=${that.data.currentMacaddress}&currentcheckAddress=${that.data.currentcheckAddress}&currentcategoryid=${e.currentTarget.dataset.categoryid}&currentcheckPointId=${that.data.currentcheckPointId}&currentcategory=${e.currentTarget.dataset.category}`
          // })
          }
          this.setData({
            deviceName:"",
            
          })
         
        }
      })}
     
  
   

  },

  //点击任务转到地点页面
  todutyList: util.throttle(function (e) {
    console.log("e", e.currentTarget.dataset.macaddress)
    console.log("ee",  e.currentTarget.dataset)
    this.setData({
      categoryid: e.currentTarget.dataset.categoryid,
      category: e.currentTarget.dataset.category,
      e_macaddress: e.currentTarget.dataset.macaddress
    })

    //获得该任务待选检查点列表
    let that = this
    wx.request({
      url: app.zhizhouUrl + '/WeChat/weChatSystem/getCheckpointAndCategory',
      method: 'POST',
      data: {
        schoolId: parseInt(app.globalData.departmentId),
        categoryId: e.currentTarget.dataset.categoryid
      },
      dataType: 'json',
      success: res => {
        console.log("res.data", res.data)
        let  deviceSeleted=false;
        res.data.map((item, index)=>{
         
          if(item.macAddress.length>=16 && item.isUse==1){
            deviceSeleted=true;
          }
        })
        this.setData({
          deviceSeleted: deviceSeleted
        })
        if(this.data.deviceSeleted==false ){
          let that = this;
        let list = res.data.filter((item) => item.isUse == 1)
        console.log("list", list)
        // 需要打开蓝牙
        if (e.currentTarget.dataset.macaddress.indexOf("00000") > -1) {
          wx.navigateTo({
            url: `/pages/dutyweekItemList/dutyweekItemList?currentMacaddress=${that.data.currentMacaddress}&currentcheckAddress=${that.data.currentcheckAddress}&currentcategoryid=${e.currentTarget.dataset.categoryid}&currentcheckPointId=${that.data.currentcheckPointId}&currentcategory=${e.currentTarget.dataset.category}`
          })
          that.setData({
            fromIndex: 0
          })
        } else {
          this.setData({
            stop: 0
          })
          if (wx.openBluetoothAdapter) {
            wx.showLoading({
              title: '正在搜索蓝牙',
              mask: true,
              showCancel: true
            })
            wx.openBluetoothAdapter({
              //开启蓝牙
              fail(res) {
                wx.showToast({
                  title: "请开启蓝牙",
                  image: "/images/warning.png",
                  duration: 2000
                })
              },
              success(res) {
                //允许打开定位
                console.log("==============进入蓝牙")
                wx.startBeaconDiscovery({
                  uuids: that.data.uuids,
                  // uuids: ["00001803-494c-4f47-4943-544543480000"],
                  success(res) {
                    console.log("res1", res)

                    // wx.onBeaconUpdate(function (res) {
                    //   console.log("开始监听~",res.beacons);
                    //   if (res.beacons.length) {
                    //     console.log("res.beacons", res.beacons);
                    //     console.log(" list", list);
                    //     var find = 0
                    //     for (let i = 0; i < res.beacons.length; i++) {
                    //       console.log("minor",res.beacons[i].minor)
                    //       console.log("accuracy",res.beacons[i].accuracy)
                    //       for (let j = 0; j < list.length; j++) {
                    //         if (`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}` == list[j].macAddress) {
                    //           console.log("macAddress", list[j].macAddress);
                    //           that.setData({
                    //             currentMacaddress: list[j].macAddress,
                    //             currentcheckAddress: list[j].address,
                    //             currentcheckPointId: list[j].id
                    //           })
                    //           find = 1
                    //           break
                    //         }
                    //       }
                    //       //搜索到的第一个蓝牙距离最近
                    //       if(find == 1){
                    //         break
                    //       }
                    //     }
                    //     if (find == 1) {
                    //       that.setData({
                    //         stop: 1,
                    //         bleConnect: 1
                    //       })
                    //       wx.hideLoading()
                    //       wx.stopBeaconDiscovery()
                    //       wx.showModal({
                    //         title: '提示',
                    //         content: '是否在' + that.data.currentcheckAddress + '开始值周',
                    //         success(res) {
                    //           if (res.confirm) {
                    //             wx.navigateTo({
                    //               url: `/pages/dutyweekItemList/dutyweekItemList?currentMacaddress=${that.data.currentMacaddress}&currentcheckAddress=${that.data.currentcheckAddress}&currentcategoryid=${e.currentTarget.dataset.categoryid}&currentcheckPointId=${that.data.currentcheckPointId}&currentcategory=${e.currentTarget.dataset.category}`
                    //             })
                    //           } else if (res.cancel) {
                    //             console.log('用户点击取消')
                    //           }
                    //         }
                    //       })
                    //     } else {
                    //       wx.hideLoading()
                    //       wx.stopBeaconDiscovery()
                    //       that.showModalOwn()
                    //     }
                    //   } else if (!res.beacons) {
                    //     console.log("首次搜索shibai", res.beacons);
                    //     wx.offBeaconUpdate((res) => {
                    //       console.log(取消监听);
                    //     })
                    //     wx.hideLoading()
                    //     wx.showModal({
                    //       title: '未搜索到蓝牙',
                    //       content: '未搜索到值周检查点蓝牙，是否重新搜索',
                    //       success(res) {
                    //         if (res.confirm) {

                    //           wx.showLoading({
                    //             title: '正在搜索蓝牙',
                    //             mask: true,
                    //             showCancel: false
                    //           })
                    //           wx.onBeaconUpdate(function (res) {
                    //             console.log("zai次搜索", res.beacons);
                    //             if (res.beacons.length) {
                    //               var find = 0
                    //               for (let i = 0; i < res.beacons.length; i++) {
                    //                 for (let j = 0; j < list.length; j++) {
                    //                   // `${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}
                    //                   if (`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}` == list[j].macAddress) {
                    //                     that.setData({
                    //                       currentMacaddress: list[j].macAddress,
                    //                       currentcheckAddress: list[j].address,
                    //                       currentcheckPointId: list[j].id
                    //                     })
                    //                     find = 1
                    //                     break
                    //                   }
                    //                 }
                    //                 break
                    //               }
                    //               if (find == 1) {
                    //                 that.setData({
                    //                   stop: 1,
                    //                   bleConnect: 1
                    //                 })

                    //                 wx.stopBeaconDiscovery()
                    //                 wx.hideLoading()
                    //                 wx.showModal({
                    //                   title: '提示',
                    //                   content: '是否在' + that.data.currentcheckAddress + '开始值周',
                    //                   success(res) {
                    //                     if (res.confirm) {
                    //                       wx.navigateTo({
                    //                         url: `/pages/dutyweekItemList/dutyweekItemList?currentMacaddress=${that.data.currentMacaddress}&currentcheckAddress=${that.data.currentcheckAddress}&currentcategoryid=${e.currentTarget.dataset.categoryid}&currentcheckPointId=${that.data.currentcheckPointId}&currentcategory=${e.currentTarget.dataset.category}`
                    //                       })
                    //                     } else if (res.cancel) {
                    //                       console.log('用户点击取消')
                    //                     }
                    //                   }
                    //                 })
                    //               } else {
                    //                 wx.hideLoading()
                    //                 wx.stopBeaconDiscovery()
                    //                 that.showModalOwn()
                    //               }
                    //             }
                    //           })
                    //         } else if (res.cancel) {
                    //           wx.hideLoading()
                    //           wx.stopBeaconDiscovery()
                    //           that.showModalOwn()
                    //         }
                    //       }
                    //     })
                    //   }
                    // })
                    wx.onBeaconUpdate(function (res) {
                      console.log("开始监听~", res.beacons);
                      if (res.beacons.length != 0) {
                        console.log("res.beacons", res.beacons);
                        console.log(" list", list);
                        let dutyWeekBea = []
                        for (let i = 0; i < res.beacons.length; i++) {
                          console.log("minor", res.beacons[i].minor)
                          console.log("accuracy", res.beacons[i].accuracy)
                          for (let j = 0; j < list.length; j++) {
                            if (`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}` == list[j].macAddress) {
                              console.log("macAddress", list[j].macAddress);
                              dutyWeekBea.push({
                                macAddress: list[j].macAddress,
                                checkAddress: list[j].address,
                                checkPointId: list[j].id
                              })
                            }
                          }
                          if(dutyWeekBea.length == 3) {
                            break
                          }
                        }
                        that.setData({
                          dutyWeekBea: dutyWeekBea
                        })
                        if (that.data.dutyWeekBea.length != 0) {
                          that.setData({
                            stop: 1,
                            bleConnect: 1
                          })
                          wx.hideLoading()
                          wx.stopBeaconDiscovery()
                        } else {
                          wx.hideLoading()
                          wx.stopBeaconDiscovery()
                          that.showModalOwn()
                        }
                      } else {
                        console.log("首次搜索shibai", res.beacons);
                        wx.offBeaconUpdate((res) => {
                          console.log(取消监听);
                        })
                        wx.hideLoading()
                        wx.showModal({
                          title: '未搜索到蓝牙',
                          content: '未搜索到值周检查点蓝牙，是否重新搜索',
                          success(res) {
                            if (res.confirm) {
                              wx.showLoading({
                                title: '正在搜索蓝牙',
                                mask: true,
                                showCancel: false
                              })
                              wx.onBeaconUpdate(function (res) {
                                console.log("zai次搜索", res.beacons);
                                if (res.beacons.length != 0) {
                                  let dutyWeekBea = []
                                  for (let i = 0; i < res.beacons.length; i++) {
                                    for (let j = 0; j < list.length; j++) {
                                      // `${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}
                                      if (`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}` == list[j].macAddress) {
                                        dutyWeekBea.push({
                                          macAddress: list[j].macAddress,
                                          checkAddress: list[j].address,
                                          checkPointId: list[j].id
                                        })
                                      }
                                    }
                                    if(dutyWeekBea.length == 3) {
                                      break
                                    }
                                  }
                                  that.setData({
                                    dutyWeekBea: dutyWeekBea
                                  })
                                  if (that.data.dutyWeekBea.length != 0) {
                                    that.setData({
                                      stop: 1,
                                      bleConnect: 1
                                    })
                                    wx.stopBeaconDiscovery()
                                    wx.hideLoading()
                                  } else {
                                    wx.hideLoading()
                                    wx.stopBeaconDiscovery()
                                    that.showModalOwn()
                                  }
                                }
                              })
                            } else if (res.cancel) {
                              wx.hideLoading()
                              wx.stopBeaconDiscovery()
                              that.showModalOwn()
                            }
                          }
                        })
                      }
                    })
                    setTimeout(function () {
                      wx.hideLoading()
                    }, 30000)
                  },
                  fail(res) {
                    console.log("------------------fail") 
                  }
                })
                // wx.getLocation({
                //   type: 'wgs84',
                //   fail: (res) => {
                //     //不允许打开定位
                //     wx.hideLoading()
                //     wx.getSetting({
                //       success(res) {
                //         //开启小程序内定位
                //         if (res.authSetting["scope.userLocation"] == false) {
                //           wx.showModal({
                //             content: '请允许小程序使用地理位置功能！您可以在【我的】页面点击【设置】按钮快捷设置。',
                //             confirmText: "去设置",
                //             success: res => {
                //               if (res.confirm) {
                //                 wx.switchTab({
                //                   url: '/pages/mine/mine'
                //                 })
                //               }
                //             }
                //           })
                //         } else {
                //           //开启系统设置定位
                //           wx.getSystemInfo({
                //             success: res => {
                //               if (res.platform == "ios") {
                //                 that.setData({
                //                   locationContent: "请在系统设置-微信里开启允许微信使用地理位置的开关"
                //                 })
                //               }
                //               wx.showModal({
                //                 title: '未允许使用地理位置',
                //                 content: that.data.locationContent,
                //                 showCancel: false
                //               })
                //             },
                //             fail: res => {
                //               wx.showModal({
                //                 title: '未允许使用地理位置',
                //                 content: that.data.locationContent,
                //                 showCancel: false
                //               })
                //             }
                //           })
                //         }
                //       }
                //     })
                //   },
                //   success: res => {
                //     //允许打开定位
                //     wx.startBeaconDiscovery({
                //       uuids: that.data.uuids,
                //       // uuids: ["00001803-494c-4f47-4943-544543480000"],
                //       success(res) {
                //         console.log("res1",res)
                //         wx.showLoading({
                //           title: '正在搜索蓝牙',
                //           mask: true,
                //           showCancel: true
                //         })
                //         let search = 0
                //         wx.onBeaconUpdate(function (res) {
                //           console.log("开始监听~",res.beacons);
                //           if (res.beacons.length) {
                //             console.log("首次搜索", res.beacons);
                //             for (let i = 0; i < res.beacons.length; i++) {
                //               var find = 0
                //               for (let j = 0; j < list.length; j++) {
                //                 // `${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}
                //                 if (`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}` == list[j].macAddress) {
                //                   console.log("list[j]", list[j].macAddress);
                //                   // let a=`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}`
                //                   that.setData({
                //                     currentMacaddress: list[j].macAddress,
                //                     currentcheckAddress: list[j].address,
                //                     currentcheckPointId: list[j].id
                //                   })
                //                   find = 1
                //                   break
                //                 }
                //               }
                //             }
                //             if (find == 1) {
                //               that.setData({
                //                 stop: 1,
                //                 bleConnect: 1
                //               })
                //               wx.hideLoading()
                //               wx.stopBeaconDiscovery()
                //               wx.showModal({
                //                 title: '提示',
                //                 content: '是否在' + that.data.currentcheckAddress + '开始值周',
                //                 success(res) {
                //                   if (res.confirm) {
                //                     wx.navigateTo({
                //                       url: `/pages/dutyweekItemList/dutyweekItemList?currentMacaddress=${that.data.currentMacaddress}&currentcheckAddress=${that.data.currentcheckAddress}&currentcategoryid=${e.currentTarget.dataset.categoryid}&currentcheckPointId=${that.data.currentcheckPointId}&currentcategory=${e.currentTarget.dataset.category}`
                //                     })
                //                   } else if (res.cancel) {
                //                     console.log('用户点击取消')
                //                   }
                //                 }
                //               })
                //             } else {
                //               wx.hideLoading()
                //               wx.stopBeaconDiscovery()
                //               that.showModalOwn()
                //             }
                //           } else if (!res.beacons) {
                //             console.log("首次搜索shibai", res.beacons);
                //             wx.offBeaconUpdate((res) => {
                //               console.log(取消监听);
                //             })
                //             wx.hideLoading()
                //             wx.showModal({
                //               title: '未搜索到蓝牙',
                //               content: '未搜索到值周检查点蓝牙，是否重新搜索',
                //               success(res) {
                //                 if (res.confirm) {

                //                   wx.showLoading({
                //                     title: '正在搜索蓝牙',
                //                     mask: true,
                //                     showCancel: false
                //                   })
                //                   wx.onBeaconUpdate(function (res) {
                //                     console.log("zai次搜索", res.beacons);
                //                     if (res.beacons.length) {
                //                       for (let i = 0; i < res.beacons.length; i++) {
                //                         var find = 0
                //                         for (let j = 0; j < list.length; j++) {
                //                           // `${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}
                //                           if ('35:11160:43' == list[j].macAddress) {
                //                             console.log("list[j]", list[j].macAddress);
                //                             // let a=`${res.beacons[i].major}:${res.beacons[i].minor}:${app.globalData.departmentId}`
                //                             that.setData({
                //                               currentMacaddress: list[j].macAddress,
                //                               currentcheckAddress: list[j].address,
                //                               currentcheckPointId: list[j].id
                //                             })
                //                             find = 1
                //                             break
                //                           }
                //                         }
                //                       }
                //                       if (find == 1) {
                //                         that.setData({
                //                           stop: 1,
                //                           bleConnect: 1
                //                         })

                //                         wx.stopBeaconDiscovery()
                //                         wx.hideLoading()
                //                         wx.showModal({
                //                           title: '提示',
                //                           content: '是否在' + that.data.currentcheckAddress + '开始值周',
                //                           success(res) {
                //                             if (res.confirm) {
                //                               wx.navigateTo({
                //                                 url: `/pages/dutyweekItemList/dutyweekItemList?currentMacaddress=${that.data.currentMacaddress}&currentcheckAddress=${that.data.currentcheckAddress}&currentcategoryid=${e.currentTarget.dataset.categoryid}&currentcheckPointId=${that.data.currentcheckPointId}&currentcategory=${e.currentTarget.dataset.category}`
                //                               })
                //                             } else if (res.cancel) {
                //                               console.log('用户点击取消')
                //                             }
                //                           }
                //                         })
                //                       } else {
                //                         wx.hideLoading()
                //                         wx.stopBeaconDiscovery()
                //                         that.showModalOwn()
                //                       }
                //                     }
                //                   })
                //                 } else if (res.cancel) {
                //                   wx.hideLoading()
                //                   wx.stopBeaconDiscovery()
                //                   that.showModalOwn()
                //                 }
                //               }
                //             })
                //           }
                //         })
                //       }
                //     })
                //   },
                //   fail: res => {
                //     wx.hideLoading()
                //   }
                // })
              }
            })
          } else {
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            })
          }
        }
        // pc测试
        // this.setData({
        //   currentMacaddress:"35:10169:13",
        //   currentcheckAddress:"食堂大厅",
        //   currentcheckPointId:1397,
        // })
        // wx.navigateTo({
        //   url: `/pages/dutyweekItemList/dutyweekItemList?currentMacaddress=${that.data.currentMacaddress}&currentcheckAddress=${that.data.currentcheckAddress}&currentcategoryid=${e.currentTarget.dataset.categoryid}&currentcheckPointId=${that.data.currentcheckPointId}&currentcategory=${e.currentTarget.dataset.category}`
        // })
        }
        
       
      }
    })

  }, 2000),
  onHide: function () {
    this.setData({
      stop: 1
    })
  },
  onUnload: function () {
    this.setData({
      stop: 1
    })
  },

  //获取班级列表信息 并处理分类
  getlist: util.throttle(function (e) {
    console.log("app.globalData.userId", app.globalData.userId)
    wx.request({
      url: app.zhizhouUrl + '/WeChat/weChatSystem/getTodayDutyWeekCondition',
      method: 'POST',
      data: {
        schoolId: parseInt(app.globalData.departmentId),
        userId: parseInt(app.globalData.userId),
      },
      dataType: 'json',
      success: res => {
        let todydata = res.data;
        let categorys = [];
        var categoryclassList = [];
        todydata.forEach((value, i) => {
          if (value.category != null) {
            let item = {
              categoryId: value.categoryId,
              categoryName: value.category,
              macaddress: value.macAddress,
              classallnum: value.numAll,
              doneNum: value.doneNum,
              nodonenum: (value.numAll != null ? value.numAll : 0) - (value.doneNum != null ? value.doneNum : 0)
            }
            categorys.push(item);
          }
        })


        this.setData({
          categorys: categorys,
          loading: 0
        })
        wx.setStorageSync('categoryclassList', res.data);
        wx.hideLoading()
      },
      fail: res => {
        wx.showToast({
          title: '网络错误！',
          image: '/images/warning.png'
        })
      }
    })
  }),


  showModalOwn: function () {
    wx.showModal({
      title: "提示",
      content: "请确保您已到达值周地点后，再次尝试",
      showCancel: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    // wx.request({
    //   url: app.zhizhouUrl + '/weChatSystem/getTodayDutyWeekCondition',
    //   method: 'POST',
    //   data: {
    //     schoolId: app.globalData.departmentId
    //     // categoryId:wx.getStorageSync('categoryclassList')[categoryindex].categoryId

    //   },
    //   dataType: 'json',
    //   success: res => {
    //     let todydata = res.data;
    //     let categorys = [];
    //     var categoryclassList = [];



    //     todydata.forEach((value, i) => {
    //       let item = {
    //         categoryId: value.categoryId,
    //         categoryName: value.category,
    //         macaddress: value.macAddress,
    //         doneNum: value.allDownNum?value.allDownNum:0,
    //         nodonenum: (value.numAll!=null?value.numAll:0) - (value.allDownNum!=null?value.allDownNum:0)
    //       }





    //       categorys.push(item);
    //     })


    //     this.setData({
    //       categorys: categorys,
    //       loading: 0

    //     })

    //     console.log(categoryclassList + "categoryclassList")

    //     wx.setStorageSync('categoryclassList', res.data);



    //     wx.hideLoading()

    //   },

    //   fail: res => {
    //     wx.showToast({
    //       title: '网络错误！',
    //       image: '/images/warning.png'
    //     })
    //   }
    // })

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getlist()


  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectAddress: function (e) {
    console.log("e", e)
    let values = e.detail.value.split(',')
    this.setData({
      selectedIndex: values[0],
      currentMacaddress: values[1],
      currentcheckAddress: values[2],
      currentcheckPointId: values[3]
    })
  },
  confirmSelection: function (e) {
    
    let that = this
    if (that.data.currentMacaddress != "" && that.data.currentcheckAddress != "" && that.data.currentcheckPointId != "") {
      wx.navigateTo({
        
        url: `/pages/dutyweekItemList/dutyweekItemList?currentMacaddress=${that.data.currentMacaddress}&currentcheckAddress=${that.data.currentcheckAddress}&currentcategoryid=${that.data.categoryid}&currentcheckPointId=${that.data.currentcheckPointId}&currentcategory=${that.data.category}&zhizhouErr=${that.data.zhizhouErr}`
      })
      console.log("===", that.data);
      this.setData({
        dutyWeekBea: [],
        selectedIndex: -1,
        currentMacaddress: "",
        currentcheckAddress: "",
        currentcheckPointId: "",
        e_macaddress:""
      })
    }

  },
  hideModal: function () {
    this.setData({
      dutyWeekBea: [],
      selectedIndex: -1,
      currentMacaddress: "",
      currentcheckAddress: "",
      currentcheckPointId: "",
      e_macaddress:""
    })
  }
})